package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.KingTravelAreaDetailEntity;
import com.ly.heart.domain.KingTravelAreaEntity;
import com.ly.heart.domain.dto.KingTravelAreaDto;
import com.ly.heart.domain.dto.StartProcessDto;
import com.ly.heart.mapper.KingTravelAreaMapper;
import com.ly.heart.service.IActModelService;
import com.ly.heart.service.IKingTravelAreaDetailService;
import com.ly.heart.service.IKingTravelAreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 出差地域 服务实现类
 * </p>
 * @author WuZH
 * @since 2024-08-18
 */
@Service
public class KingTravelAreaServiceImpl extends ServiceImpl<KingTravelAreaMapper, KingTravelAreaEntity> implements IKingTravelAreaService {

    @Resource
    private IActModelService iActModelService;
    @Resource
    private IKingTravelAreaDetailService iKingTravelAreaDetailService;

    @Override
    public List<KingTravelAreaEntity> queryList (KingTravelAreaEntity kingTravelAreaEntity) {
        return null;
    }

    @Override
    @Transactional (rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public AjaxResult draft (KingTravelAreaDto kingTravelAreaDto) {
        KingTravelAreaEntity kingTravelAreaEntity = kingTravelAreaDto.getKingTravelAreaEntity ();
        int count = this.count ();
        kingTravelAreaEntity.setDocNo ( "CCDY".concat ( String.format ( "%08d", count ) ) );
        kingTravelAreaEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        kingTravelAreaEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingTravelAreaEntity.setCreateTime ( DateUtils.getNowDate () );
        kingTravelAreaEntity.setState ( ConstUtil.STATE_IN_DRAFT );
        updateTravelAreaDetail ( kingTravelAreaDto.getKingTravelAreaDetailEntities (), kingTravelAreaEntity.getId () );
        boolean save = this.save ( kingTravelAreaEntity );
        if ( ! save ) {
            throw new CustomException ( "暂存出差地域失败！" );
        }
        return AjaxResult.success ( "暂存出差地域成功", kingTravelAreaEntity.getId () );
    }

    @Override
    public AjaxResult submit (KingTravelAreaEntity kingTravelAreaEntity) {
        if ( StringUtils.isBlank ( kingTravelAreaEntity.getId () ) ) {
            return AjaxResult.error ( "请先暂存该单据！" );
        }
        StartProcessDto startDto = new StartProcessDto ().
                setBusinessId ( kingTravelAreaEntity.getId () ).
                setBusinessType ( kingTravelAreaEntity.getBusinessType () ).setModelId ( kingTravelAreaEntity.getModelId () ).
                setBusinessName ( "出差地域".concat ( kingTravelAreaEntity.getId () ) );
        String instanceId = iActModelService.startProcess ( startDto );
        boolean update = this.update ( new LambdaUpdateWrapper<KingTravelAreaEntity> ().
                set ( KingTravelAreaEntity :: getInstanceId, instanceId ).
                set ( KingTravelAreaEntity :: getState, ConstUtil.STATE_IN_PROCESS ).
                set ( KingTravelAreaEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingTravelAreaEntity :: getUpdateTime, DateUtils.getNowDate () ).
                eq ( KingTravelAreaEntity :: getId, kingTravelAreaEntity.getId () ) );
        if ( ! update ) {
            throw new CustomException ( "提交出差地域失败！" );
        }
        return AjaxResult.success ( "提交出差地域成功" );
    }

    @Override
    public AjaxResult updateSingle (KingTravelAreaDto kingTravelAreaDto) {
        KingTravelAreaEntity kingTravelAreaEntity = kingTravelAreaDto.getKingTravelAreaEntity ();
        kingTravelAreaEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingTravelAreaEntity.setUpdateTime ( DateUtils.getNowDate () );
        updateTravelAreaDetail ( kingTravelAreaDto.getKingTravelAreaDetailEntities (), kingTravelAreaEntity.getId () );
        boolean b = this.updateById ( kingTravelAreaEntity );
        if ( ! b ) {
            throw new CustomException ( "修改出差地域失败！" );
        }
        return AjaxResult.success ( "修改出差地域成功" );
    }

    @Override
    public AjaxResult deleteSingle (String[] ids) {
        boolean update = this.update ( new LambdaUpdateWrapper<KingTravelAreaEntity> ().
                set ( KingTravelAreaEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                set ( KingTravelAreaEntity :: getUpdateTime, DateUtils.getNowDate () ).
                set ( KingTravelAreaEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                in ( KingTravelAreaEntity :: getId, ids ) );
        if ( ! update ) {
            throw new CustomException ( "删除出差地域失败！" );
        }
        return AjaxResult.success ( "删除出差地域成功" );
    }

    @Override
    public AjaxResult disabled (String id) {
        KingTravelAreaEntity travelAreaEntity = this.getById ( id );
        boolean update = this.update ( new LambdaUpdateWrapper<KingTravelAreaEntity> ().
                set ( KingTravelAreaEntity :: getIsDisabled, travelAreaEntity.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingTravelAreaEntity :: getDisabledTime, DateUtils.getNowDate () ).
                set ( KingTravelAreaEntity :: getDisabledBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                eq ( KingTravelAreaEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( travelAreaEntity.getIsDisabled () == 1 ? "禁用出差地域失败！" : "解禁出差地域失败！" );
        }
        return AjaxResult.success ( travelAreaEntity.getIsDisabled () == 1 ? "禁用出差地域成功" : "解禁出差地域成功" );
    }

    private void updateTravelAreaDetail (List<KingTravelAreaDetailEntity> kingTravelAreaDetailEntities, String travelAreaId) {
        if ( ! CollectionUtils.isEmpty ( kingTravelAreaDetailEntities ) ) {
            iKingTravelAreaDetailService.remove ( new LambdaUpdateWrapper<KingTravelAreaDetailEntity> ().eq ( KingTravelAreaDetailEntity :: getId, travelAreaId ) );
            kingTravelAreaDetailEntities.forEach ( detail -> {
                detail.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
                detail.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
                detail.setCreateTime ( DateUtils.getNowDate () );
                boolean save = iKingTravelAreaDetailService.save ( detail );
                if ( ! save ) {
                    throw new CustomException ( "更新出差地域明细失败！" );
                }
            } );
        }
    }

}
